package com.atguigu.tingshu.search.service.impl;

import cn.hutool.core.lang.Assert;
import com.atguigu.tingshu.album.AlbumFeignClient;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.user.client.UserFeignClient;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.Executor;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class ItemServiceImpl implements ItemService {
    @Autowired
    private AlbumFeignClient albumFeignClient;
    @Autowired
    private UserFeignClient userFeignClient;
    @Autowired
    private Executor threadPoolTaskExecutor;
    @Autowired
    private RedissonClient redissonClient;


    /**
     * 获取专辑详情信息
     *
     * @param albumId
     * @return
     */
    @Override
    public Map<String, Object> getAlbumInfo(long albumId) {
        //使用线程池优化远程调用
        //0.查询布隆过滤器是否包含查询专辑ID
        RBloomFilter<Long> bloomFilter = redissonClient.getBloomFilter(RedisConstant.ALBUM_BLOOM_FILTER);
        boolean flag = bloomFilter.contains(albumId);
        if (!flag) {
            throw new GuiguException(404, "访问专辑不存在");
        }

        //1 创建map对象
        /*HashMap<String, Object> map = new HashMap<>();*/
        /*Collections.synchronizedMap(map);*/
        ConcurrentHashMap<String, Object> map = new ConcurrentHashMap<>();
        //2 远程调用专辑服务，根据专辑id查询专辑
        CompletableFuture<AlbumInfo> albumInfoFuture = CompletableFuture.supplyAsync(() -> {
            AlbumInfo albumInfo = albumFeignClient.getAlbumInfo(albumId).getData();
            if (albumInfo!=null) {
                map.put("albumInfo", albumInfo);
            }else{
                Assert.notNull(albumInfo, "专辑:{}不存在！", albumId);
            }
            return albumInfo;
        }, threadPoolTaskExecutor);

        //3 远程调用专辑服务，根据专辑id查询专辑统计信息
        CompletableFuture<Void> albumStatVoFuture = CompletableFuture.runAsync(() -> {
            AlbumStatVo albumStatVo = albumFeignClient.getAlbumStatVoById(albumId).getData();
            if (albumStatVo != null) {
                map.put("albumStatVo", albumStatVo);
            } else {
                Assert.notNull(albumStatVo, "专辑:{}不存在！", albumId);
            }
        }, threadPoolTaskExecutor);

        //4 远程调用专辑服务，根据专辑id查询专辑分类
        CompletableFuture baseCategoryView = albumInfoFuture.thenAcceptAsync(albumInfo -> {
            BaseCategoryView categoryView = albumFeignClient.getCategoryView(albumInfo.getCategory3Id()).getData();
            if (categoryView != null) {
                map.put("baseCategoryView", categoryView);
            } else {
                Assert.notNull(categoryView, "专辑:{}不存在！", albumInfo.getCategory3Id());
            }
        }, threadPoolTaskExecutor);

        //5 远程调用用户服务，根据用户id查询主播信息
        CompletableFuture<Void> userInfoVoDataFuture = albumInfoFuture.thenAcceptAsync(albumInfo -> {
            UserInfoVo userInfoVo = userFeignClient.getUserInfoVo(albumInfo.getUserId()).getData();
            if (userInfoVo!=null) {
                map.put("announcer", userInfoVo);
            }else{
                Assert.notNull(userInfoVo, "专辑:{}不存在！", albumInfo.getUserId());
            }
        },threadPoolTaskExecutor);

        CompletableFuture.allOf(
                albumInfoFuture,
                albumStatVoFuture,
                baseCategoryView,
                userInfoVoDataFuture
        ).join();

        return map;
    }
}
